<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * File: models/Page_model.php
 * 
 * This demo provides a working example of using nested sets to
 * read and manipulate hierarchical data in Code Igniter
 * Please see the documentation for more info
 *
 * @author Thunder <ravenvelvet@gmail.com>
 * @copyright (c)2007 Thunder
 * @package Nested_sets
 * @subpackage Category
 */
 
class Category_model extends Nested_sets_model 
{
	public
		$table = 'categories',
		$prKey = 'id',

		$hasMany = array(
			array('table' => 'orders_items', 'fk'=>'oi_product_category'),
			array('table' => 'products_categories', 'fk'=>'pc_category_ID'),
		),
		
		$status = array(
			'1' => 'Active',
			'2' => 'Inactive'
		)
	;
    
    function Category_model() {
    
        parent::Model();
        
        // Initialise parameters to configure the Nested_sets model
        // to interact with the Category table. This extension class
        // is the thing that defines the role of the nested sets model
        
        // The three parameters passed in here are:
        //  1) the table name
        //  2) the column name representing the "left value"
        //  3) the column name representing the "right value"
        $this->setControlParams('categories','cleft','cright');
        
        // Here we tell the nested sets model which column is used as the
        // primary key. In our example, it's the auto_incrementing "categoryid"
        // column in the Category table.
        $this->setPrimaryKeyColumn('id');
        
    }
    
    /**
     * Returns a form select / drop down.
     * Provide the form field name and this method will return an HTML snippet
     * suitable for using in a form
     * @param string $fieldname The form field name to use
     * @return string Html snippet appropriate for an HTML form
     */
    function getCategorySelect($fieldname, $value = '') 
    {
        $retVal = "<select name=\"$fieldname\">\n";
        $cats_handle = $this->getTreePreOrder($this->getRoot());

        if(!empty($cats_handle['result_array'])) {
            while($this->getTreeNext($cats_handle)) 
            {
                // get indent value
                $sel = ($cats_handle['row']['id'] == $value) ? ' selected="selected"' : '';
                $indent = (@str_repeat("&nbsp;", $this->getTreeLevel($cats_handle)*2));
                $class = ($cats_handle['row']['status'] == 1) ? '' : 'class="inactive"';
                $name = ($cats_handle['row']['id'] == 1) ? 'Select' : $cats_handle['row']['name'];
                $selected = ($this->input->get_post('id_parent') == $cats_handle['row']['id']) ? 'selected="selected"' : '';
                $retVal .= "<option ".$class." ".$sel." value=\""  .$cats_handle['row']['id'] . "\" ".$selected.">$indent".$name."</option>\n";                
            }
        }
        
        $retVal .= "</select>\n";
        return $retVal;
    }
    
    /**
     * Overrides the nested_sets_model method to define the dataset field to
     * display in the tree by passing array("categoryname") to the getSubTree 
     * method
     * @return string html snippet representing the tree
     */
    function getTreeAsHTML($fields = array("name"))
    { 
        return $this->getSubTreeAsHTML($this->getRoot(), $fields);
    }

    function getTreeAsTable($fields = array("name"))
    { 
        return $this->getSubTreeAsTable($this->getRoot(), $fields);
    }

    function getTreeAsSchedule($fields = array("name"))
    { 
        return $this->getSubTreeAsSchedule($this->getRoot(), $fields);
    }
    
	function getlist($id = null)
	{
		$this->db->select('id, name, status, COUNT(product_ID) AS products');
		$this->db->from($this->category_model->table);
		$this->db->join('products_categories', 'products_categories.pc_category_ID='.$this->category_model->table.'.category_ID', 'left');
		$this->db->join('products', 'products.product_ID=products_categories.pc_product_ID', 'left');
		$this->db->group_by($this->category_model->table.'.category_ID');
				
		if ($id) 
		{
			$this->db->where($this->brand_model->prKey, $id);	
		}
		$query = $this->db->get();
		
		return $query;
	}
	
	function getSelectOptions($withoutDeleted = false)
	{
		if ($withoutDeleted)
		{
			$this->db->where('status <>', 3);
		}
		
		$query = $this->db->get($this->table);
		$options = array();
		
		foreach ($query->result() as $row)
		{
			$options[$row->category_ID] = $row->category_name; 
		}
		
		return $options;
	}

	function getSelectOptionsAsObjects()
	{
		$this->db->order_by('id_parent');
		
		$query = $this->db->get($this->table);
		$options = array();
		
		foreach ($query->result() as $row)
		{
			$options[$row->category_ID] = $row; 
		}
		
		return $options;
	}

	function delete($id)
	{
		if (!$this->hasMany($id))
		{
			parent::delete($id);
		}
		
		return false; 	
	}
}
?>
